package com.qifenqian.turbot.widget

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.view.View
import com.qifenqian.turbot.R
import kotlinx.android.synthetic.main.dialog_common.*

/**
 * @author H
 * @createTime 2019/1/14 15:47
 * @description
 * @see
 * @since
 */
class CommonDialog : Dialog {

    constructor(context: Context) : super(context) {
        init()
    }

    constructor(context: Context, themeResId: Int) : super(context, themeResId) {
        init()
    }

    constructor(context: Context, cancelable: Boolean, cancelListener: DialogInterface.OnCancelListener?) : super(context, cancelable, cancelListener) {
        init()
    }

    private fun init() {
        setContentView(R.layout.dialog_common)
    }

    fun setViews(vararg views: View): CommonDialog {
        ly_dialog_common_content.removeAllViews()
        for (view in views) {
            ly_dialog_common_content.addView(view)
        }
        return this
    }

    fun setTitleText(title: String): CommonDialog {
        tv_dialog_common_title.text = title
        return this
    }

    fun setCommonContent(content: String): CommonDialog {
        tv_dialog_common_content.text = content
        return this
    }

    fun setOk(text: String, onClickListener: DialogInterface.OnClickListener?): CommonDialog {
        btn_dialog_common_ok.text = text
        btn_dialog_common_ok.setOnClickListener {
            onClickListener?.onClick(this, 0)
        }
        return this
    }

    fun setCommonCancelable(isCancelable: Boolean): CommonDialog {
        super.setCancelable(isCancelable)
        return this
    }

    fun setCommonCanceledOnTouchOutside(isCancelable: Boolean): CommonDialog {
        super.setCanceledOnTouchOutside(isCancelable)
        return this
    }

    fun setCancel(text: String, onClickListener: DialogInterface.OnClickListener?): CommonDialog {
        btn_dialog_common_cancel.text = text
        btn_dialog_common_cancel.setOnClickListener {
            onClickListener?.onClick(this, 0)
        }
        return this
    }
}
